/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.framework.datasource.TableModel;
import com.jb.web.bean.FormField;
import com.jb.web.bean.Select;
import com.jb.web.context.WebContext;

import java.util.Iterator;

import com.jb.faces.component.UIComponent;


public class SelectRenderer extends FormItemRenderer {
  
    public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
       
        FormField formField = (FormField) itemToRender;

        Object objValue = formField.getModelBean();

        if (objValue == null) {
            String strValue = formField.getValue();

            if (isEmptyString(strValue)) {
                objValue = evaluate(pCtx, formField, formField.getDefaultValue());
            } else {
                objValue = evaluate(pCtx, formField, strValue);
            }
        }
        
        StringBuffer html = new StringBuffer("");
        objValue = (objValue == null) ? "" : objValue;
        

        if (formField.isHidden()) {
            return hiddenField(pCtx, formField, objValue.toString());
        }

        if (isVisible(pCtx, formField)) {
            html.append(optionField(pCtx, formField, objValue.toString()));
        } else {
            html.append("<INPUT type =\"hidden\" name=\"" + formField.getFieldName(pCtx) + "\"")
                .append(addTagProperty(" value", objValue.toString(), null)).append(">");
        }
       
        return html;
    }

    private String optionField(WebContext pCtx, FormField formItem, String value) {
        Select select = (Select) formItem;
        String clientId = select.getFieldName(pCtx);
        StringBuffer html =new StringBuffer();
        boolean editable = isEditable(pCtx, select);
        if(editable)
            html.append("<span ").append(getEditScript(pCtx, select)).append(" >");
        else
            html.append("<span ").append(" >");
            
        html.append("<SELECT name=\"" + clientId + "\"");

        if (!editable) {
            html.append(getEditScript(pCtx, select));
            html.append(addTagProperty("onchange", evaluate(pCtx, select, select.getOnChange()), null));
        }

        html.append(addTagProperty("multiple", "", null));

        if (pCtx.isRunTime()) {
            html.append(addTagProperty("disabled", select.isEnabled() ? "" : "disabled", null));
        }

        html.append(addTagProperty("style", select.getStyle(pCtx), null));
        html.append(addTagProperty("class", select.getStyleClass(), null)).append(">\n");

        TableModel tableData = optionData(pCtx, select, select.getValueOptions());

        //String defaultValue = value;
        if (tableData != null) {
            for (int i = 0; i < tableData.getMaxValue(); i++) {
                try {
                    Iterator row = tableData.getItem(i);
                    String dbName =  row.next().toString();
                    String dbValue = row.next().toString();

                    if ((dbValue != null) && (dbValue.equals(value))) {
                        html.append("<OPTION value=\"" + dbValue + "\" selected>" + dbName + "</OPTION>\n");
                    } else {
                        html.append("<OPTION value=\"" + dbValue + "\">" + dbName + "</OPTION>\n");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        html.append("</SELECT>");

       // if (isEditable(pCtx, select)) {
            html.append("</span>");
        //}

        return html.toString();
    }
}
